﻿using EmbeddedSharp.Core;
using EmbeddedSharp.Scripting;

namespace Zove.Scripting.Core {
    /// <summary>
    /// the parser factory
    /// </summary>
    public class ZoveScriptExpressionParserFactory : AbstractExpressionParserFactory
    {

        protected readonly IContext context;

        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="context">context</param>
        public ZoveScriptExpressionParserFactory(
            IContext context)
            : base() {
            this.context = context;
        }

        /// <summary>
        /// Create a parser
        /// </summary>
        /// <param name="expr"></param>
        /// <returns></returns>
        public override ExpressionParser CreateParser(string expr) {
            ContextExpressionParser cp = new ContextExpressionParser(expr, context)
                                   {
                                       ConstantManager = DefaultDependencyManager.Instance.ConstantManager,
                                       FunctionManager = context.GetFunctionManager(),
                                       ExpressionParserFactory = this,
                                       Context = this.context
                                   };

            return cp;
        }

        /// <summary>
        /// Create a parser
        /// </summary>
        /// <param name="expr">expression</param>
        /// <param name="context">context</param>
        /// <returns>expression parser</returns>
        public override ExpressionParser CreateParser(string expr, object context) {
            return this.CreateParser(expr);
        }
    }
}
